import storage from 'good-storage';
const SELLER_KEY = '__seller__';
/**
 * @desc 已收藏 本地缓存
 * @param id -- -- - 商家id key(favorite) value(true)
 * @example saveToLocal(445454,'favorite',true)
 * @return 无返回值
 */
export function saveToLocal(id, key, val) {
  const seller = storage.get(SELLER_KEY, {});
  if (!seller[id]) seller[id] = {};
  seller[id][key] = val;
  storage.set(SELLER_KEY, seller);
}

/**
 * @desc 已收藏 返回储存的本地缓存
 * @param id -- sellID def--默认值
 * @example loadFromLocal(1223,'favorite',false)
 * @return true false
 */
export function loadFromLocal(id, key, def) {
  const seller = storage.get(SELLER_KEY, {});
  if (!seller[id]) return def;
  if (Array.isArray(seller[id]) && !seller[id].length) return def;
  if (Object.keys(seller[id]).length === 0) return def;
  return seller[id][key] || def;
}

export function removeFromLocal(id, key) {
  const seller = storage.get(SELLER_KEY, {});

  if (seller[id] && seller[id][key]) storage.remove(key);
}
